package gxt.visual.resource;

import gxt.visual.event.VisualEvents;
import gxt.visual.util.CommonChecks;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * This is a shared resource populated by calling the server; <br>
 * @author eugenp
 */
public abstract class SharedServerResource< E > extends SharedResource< E >{
	/** This is a flag that becomes that knows if the server call has been made or not <br> */
	protected boolean serverCalledForResource;
	/** This is the callback object to be used for the server call */
	protected AsyncCallback< E > callback;
	
	protected SharedServerResource(){
		super();
	}
	
	//
	@Override
	protected final void initializeResources(){
		this.callback = new AsyncCallback< E >(){
			public final void onSuccess( final E result ){
				CommonChecks.checkNullArgs( result );
				SharedServerResource.this.resource = result;
				SharedServerResource.this.fireEvent( VisualEvents.ResourceAvailable );
			}
			
			public final void onFailure( final Throwable caught ){
				throw new IllegalStateException( caught );
			}
		};
		
		this.makeServerCall();
	}
	
	public abstract void makeServerCall();
	
	public final boolean getStatusOfCall(){
		return this.serverCalledForResource;
	}
	
}
